<template>
  <div ref="mainCon" class="container">
    <topTitle v-if="!hiddleToolBar" :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="姓名" class="row-padding-bottom">
              <el-input
                placeholder="请输入姓名"
                clearable
                v-model="filterField.nickName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="登录账号" class="row-padding-bottom">
              <el-input
                placeholder="请输入登录账号"
                clearable
                v-model="filterField.userName"
              ></el-input>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="管辖项目" class="row-padding-bottom">
                <el-input
                  placeholder="请输入项目名称"
                  clearable
                  v-model="filterField.projectName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="账号状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.enabled"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in stateOptions"
                    :key="item.id"
                    :label="item.text"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-form-item>
                        <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchList" class="commonBtn"></el-button>
                    </el-form-item> -->
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :dataLen="projectRightList.length"
                :filterField="filterField"
                api="api-s/report/export_JC_CS_USER_PRO_MANAGE"
              ></exportBtn>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="projectRightList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column prop="nickName" label="姓名" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.nickName"
                :columnName="'nickName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="userName"
            label="登录账号"
            :formatter="formatTd"
            min-width="132"
          ></el-table-column>
          <el-table-column
            prop="tel"
            label="手机号"
            :formatter="formatTd"
            min-width="140"
          ></el-table-column>
          <!-- <el-table-column prop="projectName" label="管辖项目" min-width="180" :formatter=formatTd></el-table-column> -->
          <el-table-column prop="管辖项目" label="管辖项目" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="typeDesc"
            label="岗位"
            :formatter="formatTd"
            min-width="110"
          ></el-table-column>
          <el-table-column
            prop="enabledDesc"
            label="账号状态"
            :formatter="formatTd"
            min-width="100"
          >
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="200">
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="toResetPassWord(scope.$index, scope.row)"
                >重置密码</el-link
              >
              <el-link
                type="primary"
                @click="toChangeTel(scope.$index, scope.row)"
                >修改手机号</el-link
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关数据"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
      <!-- 重置密码 -->
      <el-dialog
        title="重置密码"
        :visible.sync="formVisible"
        :close-on-click-modal="false"
        @close="cancel('formData')"
        custom-class="brand-dialog-min-width"
        width="640px"
        :modal-append-to-body="false"
        :append-to-body="true"
      >
        <el-form
          :model="formData"
          label-width="120px"
          class="mrT8"
          :rules="formRules"
          ref="formData"
        >
          <el-form-item label="原手机号：">
            <el-input
              v-model="formData.oldTel"
              autocomplete="off"
              placeholder="原手机号"
              class="dialogSingleInp"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="修改后：" prop="tel">
            <el-input
              v-model="formData.tel"
              autocomplete="off"
              placeholder="请输入新密保手机号"
              class="dialogSingleInp"
              @blur="formData.tel = changeValue($event)"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="cancel('formData')" class="cancel"
            >取消</el-button
          >
          <el-button
            type="primary"
            @click.native="toReset('formData')"
            class="save"
            :loading="canLogin"
            >保存</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import emptyTable from '@/components/publicCom/emptyTable'
import Pagination from '@/components/publicCom/pagination'
import topTitle from '@/components/publicCom/topTitle'
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'projectAccountManage',
  mixins: [scrollTable],
  components: {
    emptyTable,
    Pagination,
    topTitle,
  },
  data() {
    return {
      titleText: '项目账号管理',
      advanced: false,
      stateOptions: [
        { id: '1', text: '正常' },
        { id: '0', text: '禁用' },
      ],
      //表格数据
      projectRightList: [],
      requestUrl: 'api-u/userpro/finduserproject',
      //搜索条件
      filterField: {
        nickName: '', //姓名
        userName: '', //登录账号
        projectName: '', //项目名称
        enabled: '', //账号状态
      },
      pageSize: 20,
      pageNo: 1,
      isShow: false, //列表加载
      loading: true,
      formData: {
        oldTel: '',
        tel: '',
        userId: '',
        updateType: '01',
      },
      formRules: {
        tel: [
          {
            required: true,
            pattern: /^\d{11}$/,
            message: '手机号必须为11位数字',
            trigger: 'blur',
          },
        ],
      },
      formLabelCardWidth: '125px',
      canLogin: false,
      formVisible: false,
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.projectRightList = data
      this.loading = false
      if (this.projectRightList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    //修改手机号
    toChangeTel(index, row) {
      this.formVisible = true
      this.formData.oldTel = row.tel
      this.formData.userId = row.id
    },
    toReset() {
      this.canLogin = true
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.changeTel()
        } else {
          this.canLogin = false
        }
      })
    },
    changeTel() {
      this.axios({
        method: 'post',
        data: this.formData,
        url: 'api-u/userpro/updateuserpasswordortel',
      })
        .then(
          (response) => {
            this.canLogin = false
            if (!response.data.hasError) {
              this.$message({
                message: '修改成功',
                type: 'success',
              })
              this.formVisible = false
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {
            this.canLogin = false
          }
        )
        .catch((error) => {
          this.canLogin = false
        })
    },
    cancel(formData) {
      this.$refs['formData'].resetFields()
      this.formVisible = false
    },
    //重置密码
    toResetPassWord(index, row) {
      if (!row.tel) {
        this.$message({
          message: '账号手机号为空，请先补充手机号',
          type: 'warning',
        })
      } else {
        this.resetPassWord(index, row)
      }
    },
    resetPassWord(index, row) {
      var tel = row.tel.substring(row.tel.length - 6)
      var html =
        "<div style='color:red'>确认重置，账号{<span style='color:#606266'>" +
        row.userName +
        '</span>}的密码将重置为手机号后6位：' +
        tel +
        '</div>' +
        '<div>系统将把账号和密码发送至账号关联的手机号，如手机号错误请修改</div>'
      this.$confirm(html, '重置密码', {
        dangerouslyUseHTMLString: true,
        // type: "warning",
        confirmButtonText: '确认重置',
        cancelButtonText: '取消',
      })
        .then((action) => {
          this.changePassWord(row)
        })
        .catch(() => {
          // console.log("取消");
        })
    },
    changePassWord(row) {
      this.axios({
        method: 'post',
        data: {
          updateType: '05',
          userId: row.id,
        },
        url: 'api-u/userpro/updateuserpasswordortel',
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.$message({
                message: '重置成功',
                type: 'success',
              })
              this.formVisible = false
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
  },
}
</script>

<style lang="less" scoped>
.container {
  .topContent {
    background: #e4ebf1;
    height: 67px;
    line-height: 40px;
    padding: 15px 24px;
    margin: 24px;
    .titleTop {
      font: bold 18px Microsoft YaHei;
      color: #333333;
    }
    .alignRight {
      text-align: right;
    }
    .createSalaryBut {
      background: #009588;
      border: 1px solid #009588;
      padding: 9px 23px;
      &:hover {
        opacity: 0.8;
      }
    }
  }
  .mainCon {
    top: 198px;
  }
}
@import '../../../static/css/filterArea.less';
</style>
